package com.ruoyi.thread;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;
import org.springframework.util.concurrent.ListenableFuture;

import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;
/**
 *
 * @author DEREK
 */
@SuppressWarnings("serial")
@Component
public class VisibleThreadPoolTaskExecutor extends ThreadPoolTaskExecutor{

	 private static final Logger logger = LoggerFactory.getLogger(VisibleThreadPoolTaskExecutor.class);

	    public String info() {
	        ThreadPoolExecutor executor = getThreadPoolExecutor();
	        if (executor == null) return "线程池不存在";

	        String info = "线程池" + this.getThreadNamePrefix() +
	                "中，总任务数为 " + executor.getTaskCount() +
	                " ，已处理完的任务数为 " + executor.getCompletedTaskCount() +
	                " ，目前正在处理的任务数为 " + executor.getActiveCount() +
	                " ，缓冲队列中任务数为 " + executor.getQueue().size();

	        logger.info(info);

	        return info;
	    }

	    @Override
	    public void execute(Runnable task) {
	        info();
	        super.execute(task);
	    }

	    @SuppressWarnings("deprecation")
		@Override
	    public void execute(Runnable task, long startTimeout) {
	        info();
	        super.execute(task, startTimeout);
	    }

	    @Override
	    public Future<?> submit(Runnable task) {
	        info();
	        return super.submit(task);
	    }

	    @Override
	    public <T> Future<T> submit(Callable<T> task) {
	        info();
	        return super.submit(task);
	    }

	    @Override
	    public ListenableFuture<?> submitListenable(Runnable task) {
	        info();
	        return super.submitListenable(task);
	    }

	    @Override
	    public <T> ListenableFuture<T> submitListenable(Callable<T> task) {
	        info();
	        return super.submitListenable(task);
	    }
}
